package com.qzsoft.zhjd.monitoring.modules.zlm.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.qzsoft.zhjd.monitoring.config.ZLMConstants;
import com.qzsoft.zhjd.monitoring.config.ZLMMediaConfig;
import com.qzsoft.zhjd.monitoring.config.ZLMServerConfig;
import com.qzsoft.zhjd.monitoring.modules.videomonitor.entity.VideoMonitorEntity;
import com.qzsoft.zhjd.monitoring.modules.videomonitor.service.VideoMonitorService;
import com.qzsoft.zhjd.monitoring.utils.ZLMRestfulUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ZLMServerManager
 * @Description ZLM 服务器管理
 * @author linhuawei
 * @since 2022-03-22
 */
@Slf4j
@Component
public class ZLMServerManager {

    @Autowired
    private ZLMRestfulUtils zlmRestfulUtils;

    @Autowired
    private ZLMMediaConfig zlmMediaConfig;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private VideoMonitorService videoMonitorService;

    /**
     * @Title getServerConfig
     * @Description 获取服务器配置
     */
    public void getServerConfig() {
        ZLMServerConfig zlmServerConfig = zlmRestfulUtils.getServerConfig();
        while (null == zlmServerConfig) {
            log.error("连接ZLM获取配置失败，1s后重新读取");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            zlmServerConfig = zlmRestfulUtils.getServerConfig();
        }

        log.info("连接ZLM获取配置成功");
        updateCacheServerConfig(zlmServerConfig);
    }

//    /**
//     * @Title addEnableStreamProxy
//     * @Description 添加启用状态的流代理
//     */
//    public void addEnableStreamProxy() {
//        log.info("查询启用状态摄像头,添加推流代理");
//        List<VideoMonitorEntity> list = videoMonitorService.listAllEnable();
//        if (null != list) {
//            for (VideoMonitorEntity entity : list) {
//                if (zlmRestfulUtils.addStreamProxy(entity)) {
//                    log.info("流代理[app = " + entity.getApp() + ",stream = " + entity.getStream() + "]添加成功");
//                } else {
//                    log.info("流代理[app = " + entity.getApp() + ",stream = " + entity.getStream() + "]添加失败");
//                    videoMonitorService.disable(entity.getId());
//                }
//            }
//        }
//    }

//    /**
//     * @Title closeStreamProxy
//     * @Description 关闭推流
//     * @param app
//     * @param stream
//     */
//    public void closeStreamProxy(String app, String stream) {
//        if (zlmRestfulUtils.closeStreamsByForce(app, stream)) {
//            videoMonitorService.disable(app, stream);
//        }
//    }

    /**
     * @Title getCacheServerConfig
     * @Description 
     * @return
     */
    public ZLMServerConfig getCacheServerConfig() {
        Object obj = redisTemplate.opsForValue().get(ZLMConstants.MEDIA_SERVER_PREFIX);
        return null == obj ? null : (ZLMServerConfig) obj;
    }

    /**
     * @Title updateCacheServerConfig
     * @Description 更新配置信息
     * @param zlmServerConfig 配置信息
     */
    public void updateCacheServerConfig(ZLMServerConfig zlmServerConfig) {
        zlmServerConfig.setLocalIP(zlmMediaConfig.getIp());
        zlmServerConfig.setWanIp(
                StringUtils.isEmpty(zlmMediaConfig.getWanIp()) ? zlmMediaConfig.getIp() : zlmMediaConfig.getWanIp());
        zlmServerConfig.setHttpPort(zlmMediaConfig.getHttpPort());

        if (!StringUtils.isEmpty(zlmMediaConfig.getHttpSSlPort())) {
            zlmServerConfig.setHttpSSLport(zlmMediaConfig.getHttpSSlPort());
        }

        if (!StringUtils.isEmpty(zlmMediaConfig.getRtspPort())) {
            zlmServerConfig.setRtspPort(zlmMediaConfig.getRtspPort());
        }

        if (!StringUtils.isEmpty(zlmMediaConfig.getRtspSSLPort())) {
            zlmServerConfig.setRtspSSlport(zlmMediaConfig.getRtspSSLPort());
        }

        if (!StringUtils.isEmpty(zlmMediaConfig.getRtmpPort())) {
            zlmServerConfig.setRtmpPort(zlmMediaConfig.getRtmpPort());
        }

        if (!StringUtils.isEmpty(zlmMediaConfig.getRtmpSSlPort())) {
            zlmServerConfig.setRtmpSslPort(zlmMediaConfig.getRtmpSSlPort());
        }

        if (!StringUtils.isEmpty(zlmMediaConfig.getRtpProxyPort())) {
            zlmServerConfig.setRtpProxyPort(zlmMediaConfig.getRtpProxyPort());
        }

        redisTemplate.opsForValue().set(ZLMConstants.MEDIA_SERVER_PREFIX, zlmServerConfig);
    }

    /**
     * @Title onStreamNotFound
     * @Description 视频流不存在，添加推流代理
     * @param app
     * @param stream
     */
    public void onStreamNotFound(String app, String stream) {
        VideoMonitorEntity entity = videoMonitorService.getByAppAndStream(app, stream);
        if(null != entity) {
            zlmRestfulUtils.addStreamProxy(entity);
        }
    }

}
